﻿using King.Box.AdminWeb.Filter;
using King.Box.AdminWeb.Logic;
using King.Box.Models.Db.Sys;
using King.Box.Models.Query.Sys;
using King.Box.Models.Util;
using King.Box.Models.View.Sys;
using King.Box.Utils.Global;
using King.Box.Utils.Utils;
using PsCat.Aspet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace King.Box.AdminWeb.Controllers
{
    [Login]
    public class SysController : Controller
    {
        public SysService _sysService;
        public SysController()
        {
            _sysService = new SysService();
        }
        
        #region 菜单
        [UrlCat]
        public ActionResult SysMenuList()
        {
            ViewData.Add("Level", new SelectList(Util.ParseEnumToDictionary<GlobalEnum.MenuLevel>(GlobalConst.AllSelect),
                GlobalConst.Value, GlobalConst.Key, GlobalConst.AllSelectValue));

            var menus = _sysService.SelectCanParentMenu();
            menus.Insert(0, new Sys_MenuData() { Id = GlobalConst.PleaseSelectValue, Name = GlobalConst.AllSelect, LevelName = string.Empty });
            var parentMenu = new SelectList(menus, "Id", "Name", "LevelName", GlobalConst.AllSelectValue);
            ViewData.Add("Parent", parentMenu);

            return View();
        }

        [UrlCat]
        [HttpPost]
        public JsonResult SysMenuList(SysMenuQuery query)
        {
            return Json(_sysService.SelectMenu(query));
        }

        [UrlCat]
        public ActionResult SysMenuTree()
        {
            return View();
        }

        [HttpPost]
        [UrlCat]
        public JsonResult SysMenuTreeList()
        {
            return Json(_sysService.SelectMenuLevel());
        }

        [UrlCat]
        public ActionResult SysMenuEdit(int? id)
        {
            Sys_Menu menu = null;
            if (id.HasValue)
                menu = _sysService.GetMenu(id.Value);

            {
                var menuLevels = new SelectList(Util.ParseEnumToDictionary<GlobalEnum.MenuLevel>(GlobalConst.PleaseSelect),
                        GlobalConst.Value, GlobalConst.Key, menu == null ? GlobalConst.PleaseSelectValue : menu.Level);

                ViewData.Add("LevelData", menuLevels);

                var menus = _sysService.SelectCanParentMenu();
                menus.Insert(0, new Sys_MenuData() { Id = GlobalConst.PleaseSelectValue, Name = GlobalConst.PleaseSelect, LevelName = string.Empty });
                var parentMenu = new SelectList(menus, "Id", "Name", "LevelName", menu == null ? GlobalConst.PleaseSelectValue : menu.ParentId);

                ViewData.Add("ParentData", parentMenu);
            }
            return View(menu);
        }

        [HttpPost]
        [UrlCat]
        public JsonResult SysMenuEdit(Sys_Menu menu)
        {
            return Json(_sysService.SaveMenu(menu));
        }
        #endregion

        #region 权限
        [UrlCat]
        public ActionResult SysPermissionList()
        {
            ViewData.Add("ReferType", new SelectList(Util.ParseEnumToDictionary<GlobalEnum.SysPermissionReferType>(GlobalConst.AllSelect),
                GlobalConst.Value, GlobalConst.Key, GlobalConst.AllSelectValue));

            return View();
        }

        [HttpPost]
        [UrlCat]
        public JsonResult SysPermissionList(SysPermissionQuery query)
        {
            return Json(_sysService.SelectSysPermissionDetail(query));
        }
        #endregion

        #region 用户
        [UrlCat]
        public ActionResult SysUserList()
        {
            return View();
        }

        [HttpPost]
        [UrlCat]
        public JsonResult SysUserList(SysUserQuery query)
        {
            return Json(_sysService.SelectUser(query));
        }
        #endregion
    }
}